<template>
	<view class="uni-tab-bar">
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft" style="background: #fff;">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex==index ? 'active' : '']" :id="tab.id" :data-current="index" @tap="tapTab" v-cloak>{{tab.name}}</view>
		</scroll-view>
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab" :style="{'height': systemInfo +'px'}">
			<swiper-item v-for="(tab,index1) in newsitems" :key="index1">
				<scroll-view class="list" scroll-y @scrolltolower="loadMore(index1)">
					<block v-for="(newsitem,index2) in tab.data" :key="index2">
					<media-list :data="newsitem"  @cancel="cancel(newsitem.id)" @confirm='confirm(newsitem.id)' @log="log(newsitem.id)" @evaluate="evaluate(newsitem.id)" @del="del(newsitem.id)" @pay="pay(newsitem.id)" @click="goDetail(newsitem.id)"></media-list>
					</block>
					<view class="uni-tab-bar-loading" v-if="tab.length == pagesize">
						<uni-load-more :loadingType="tab.loadingType" :contentText="loadingText"></uni-load-more>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	import mediaList from '../../../../components/tab-nvue/mediaList.vue';
	import uniLoadMore from '../../../../components/uni-load-more.vue';
	export default {
		components: {
			mediaList,
			uniLoadMore
		},
		data() {
			return {
				openid:'',
				systemInfo:[],
				data0:[],
				data1:[],
				data2:[],
				data3:[],
				data4:[],
				index0:1,
				index1:1,
				index2:1,
				index3:1,
				index4:1,
				pagesize:0,
				tabIndex: 0,
				scrollLeft: 0,
				isClickChange: false,
				loadingText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多订单了"
				},
				newsitems: [{
					"loadingType":0,
					"data":[]
				},{
					"loadingType":0,
					"data":[]
				},{
					"loadingType":0,
					"data":[]
				},{
					"loadingType":0,
					"data":[]
				},{
					"loadingType":0,
					"data":[]
				}],
				tabBars: [{
					name: '全部',
					id: 'guanzhu'
				}, {
					name: '待付款',
					id: 'tuijian'
				}, {
					name: '待发货',
					id: 'tiyu'
				}, {
					name: '待收货',
					id: 'redian'
				}, {
					name: '已完成',
					id: 'caijing'
				}]
			}
		},
		onLoad: function(e) {
			// uni.showLoading()
			this.tabIndex = -1
			setTimeout(() => {
				this.tabIndex = e.index
			}, 1000);
			uni.getSystemInfo({
				success: (res) => {
					this.systemInfo = res.windowHeight -50
				}
			})
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data
					this.setAjax()
				},
				fail: () => {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			});
		},
		onNavigationBarButtonTap:function(e){
			uni.navigateTo({
				url: 'recycle/recycle'
			});
    },
		methods: {
			// 加载数据
			setAjax(){
				this.setData0()
				this.setData1()
				this.setData2()
				this.setData3()
				this.setData4()
			},
			setData0(){
				uni.request({
					url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.get_list&app=1&page='+this.index0,
					method: 'POST',
					data: {
					'openid':this.openid,
					'credit1':1,
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						console.log(res)
				uni.hideLoading()
						if(this.index0 == 1){
							this.data0 = res.data.result.list
							this.newsitems[0].data = this.data0
						}else{
							if(res.data.result.list.length == 0){
								this.newsitems[0].loadingType = 2;
								return;
							}else{
								this.data0 = this.data0.concat(res.data.result.list)
								this.newsitems[0].data = this.data0
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
			},
			setData1(){
				uni.request({
					url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.get_list&app=1&page='+this.index1+'&status=0',
					method: 'POST',
					data: {
						'openid':this.openid,
						'credit1':1,
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						console.log(res)
				uni.hideLoading()
						this.pagesize = res.data.result.pagesize
						if(this.index1 == 1){
							this.data1 = res.data.result.list
							this.newsitems[1].data = this.data1
						}else{
							if(res.data.result.list.length == 0){
								this.newsitems[1].loadingType = 2;
								return;
							}else{
								this.data1 = this.data1.concat(res.data.result.list)
								this.newsitems[1].data = this.data1
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
			},
			setData2(){
				uni.request({
					url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.get_list&app=1&page='+this.index2+'&status=1',
					method: 'POST',
					data: {
						'openid':this.openid,
						'credit1':1,
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()

						if(this.index2 == 1){
							this.data2 = res.data.result.list
							this.newsitems[2].data = this.data2
						}else{
							if(res.data.result.list.length == 0){
								this.newsitems[2].loadingType = 2;
								return;
							}else{
								this.data2 = this.data2.concat(res.data.result.list)
								this.newsitems[2].data = this.data2
							}
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
			},
			setData3(){
				uni.request({
					url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.get_list&app=1&page='+this.index3+'&status=2',
					method: 'POST',
					data: {
						'openid':this.openid,
						'credit1':1,
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()

						if(this.index3 == 1){
							this.data3 = res.data.result.list
							this.newsitems[3].data = this.data3
						}else{
							if(res.data.result.list.length == 0){
								this.newsitems[3].loadingType = 2;
								return;
							}else{
								this.data3 = this.data3.concat(res.data.result.list)
								this.newsitems[3].data = this.data3
							}
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setData4(){
				uni.request({
					url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.get_list&app=1&page='+this.index4+'&status=3',
					method: 'POST',
					data: {
						'openid':this.openid,
					  'credit1':1,
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()

						if(this.index4 == 1){
							this.data4 = res.data.result.list
							this.newsitems[4].data = this.data4
						}else{
							if(res.data.result.list.length == 0){
								this.newsitems[4].loadingType = 2;
								return;
							}else{
								this.data4 = this.data4.concat(res.data.result.list)
								this.newsitems[4].data = this.data4
							}
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 加载更多
			loadMore(e) {
				this.newsitems[e].loadingType = 1;
				this.addData(e);
			},
			addData(e) {
				if(e == 0){
					this.index0 ++
					this.setData0()
				}else if(e == 1){
					this.index1 ++
					this.setData1()
				}else if(e == 2){
					this.index2 ++
					this.setData2()
				}else if(e == 3){
					this.index3 ++
					this.setData3()
				}else if(e == 4){
					this.index4 ++
					this.setData4()
				}
			},
			// 操作功能
			cancel(id) {
				//取消订单
				var itemList1 = ['我不想买了', '信息填写错误，重新拍', '其他原因']
				uni.showActionSheet({
					itemList: itemList1,
					success: (e) => {
						uni.request({
							url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.op.cancel&app=1',
							method: 'POST',
							data: {
								'openid':this.openid,
								'id':id,
								'remark':itemList1[e.tapIndex],
							},
							header: {'Content-Type': 'application/x-www-form-urlencoded'},
							success: res => {
								console.log(res)
								uni.showToast({
									title:res.data.result.message,
									icon:"none"
								})
								uni.showLoading()
								this.setAjax()
								this.tabIndex = -1
								setTimeout(() => {
									uni.hideLoading()
									this.tabIndex = 0
								}, 1000);
							},
							fail: () => {},
							complete: () => {}
						});
					}
				})
			},
			del(id) {
				// 删除订单
				uni.showModal({
					content: '确认要删除该订单吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.op.delete&app=1&orderid='+id+'&userdeleted=1',
								method: 'POST',
								data: {'openid':this.openid},
								header: {'Content-Type': 'application/x-www-form-urlencoded'},
								success: res => {
									uni.showLoading()
									this.setAjax()
									this.tabIndex = -1
									setTimeout(() => {
										uni.hideLoading()
										this.tabIndex = 0
									}, 1000);
								},
								fail: () => {},
								complete: () => {}
							});
						}
					}
				})
			},
			confirm(id){
				// 确认收货
				uni.showModal({
					content: '确认已收到货了吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'https://bhh.ax1319.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=order.op.finish&app=1&orderid='+id,
								method: 'POST',
								data: {'openid':this.openid},
								header: {'Content-Type': 'application/x-www-form-urlencoded'},
								success: res => {
									uni.showLoading()
									this.setAjax()
									setTimeout(() => {
										uni.hideLoading()
										this.tabIndex = 4
									}, 1000);
								},
								fail: () => {},
								complete: () => {}
							});
						}
					}
				})
			},
			// 跳转操作
			goDetail(id) {
				uni.navigateTo({
					url: 'detail/detail?id=' + id
				})
			},
			evaluate(id){
				uni.navigateTo({
					url: 'comment/comment?id='+id
				});
			},
			log(id){
				uni.navigateTo({
					url: 'express/express?id='+id
				});
			},
			pay(id){
				uni.navigateTo({
					url:'pay/pay?id='+id
				});
			},
			// tab切换功能
			async changeTab(e) {
				let index = e.target.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			async tapTab(e) { //点击tab-bar
				if (this.tabIndex == e.target.dataset.current) {
					return false;
				} else {
					this.isClickChange = true;
					this.tabIndex = e.target.dataset.current
				}
			}
		}
	}
</script>

<style>uni-page-body{height:100%;}</style>
